package com.yango.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.commonutil.result.Result;
import com.yango.commonutil.utils.JwtUtils;
import com.yango.order.entity.Order;
import com.yango.order.entity.vo.OrderQuery;
import com.yango.order.service.OrderService;
import com.yango.serviceutil.vo.CourseFrontVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单微服务 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-08-04
 */
@Api(description = "订单微服务 前端控制器")
@RestController
@RequestMapping("/orderService/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //根据课程id和用户id创建订单，返回订单id
    @ApiOperation(value = "根据课程id和用户id创建订单，返回订单id")
    @PostMapping("/createOrder/{courseId}")
    public Result createOrder(@ApiParam(name = "", value = "订单id", required = true)
                              @PathVariable String courseId,
                              HttpServletRequest request) {
        if (StringUtils.isEmpty(request)) {//用户未登录
            return Result.error().message("您还未登录!请先登录!");
        }
        String orderId = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return Result.ok().data("orderId", orderId);
    }

    //根据订单号获取订单信息
    @ApiOperation(value = "根据订单号获取订单信息")
    @GetMapping("/getOrderByOrderNo/{orderNo}")
    public Result getOrderByOrderNo(@ApiParam(name = "orderNo", value = "订单号", required = true)
                                    @PathVariable String orderNo) {
        //条件构造器
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no", orderNo);//根据订单号查询
        //查询
        Order order = orderService.getOne(wrapper);
        return Result.ok().data("order", order);
    }

    //根据课程id和用户id查询订单状态
    @ApiOperation(value = "根据课程id和用户id查询订单状态")
    @GetMapping("/isBuyCourse/{courseId}/{memberId}")
    public boolean isBuyCourse(@ApiParam(name = "courseId", value = "课程id", required = true)
                               @PathVariable String courseId,
                               @ApiParam(name = "memberId", value = "用户id", required = true)
                               @PathVariable String memberId) {
        //条件构造器
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.eq("member_id", memberId);
        wrapper.eq("status", 1);//支付状态为1表示已支付
        //查询--查询记录条数，如果有数据，则表示支付成功
        int count = orderService.count(wrapper);
        if (count > 0) {//已支付
            return true;
        } else {//未支付
            return false;
        }
    }

    //分页-条件查询所有订单
    @ApiOperation(value = "分页-条件查询所有订单")
    @PostMapping("/pageOrderList/{current}/{limit}")
    public Result pageOrderList(@ApiParam(name = "current", value = "当前页", required = true)
                                @PathVariable long current,
                                @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                                @PathVariable long limit,
                                @ApiParam(name = "orderQuery", value = "查询对象", required = false)
                                @RequestBody OrderQuery orderQuery) {
        //创建page对象
        Page<Order> page = new Page<>(current, limit);
        //调用方法
        orderService.pageOrderList(page, orderQuery);
        List<Order> records = page.getRecords();//记录集合
        long total = page.getTotal();//总记录数
        return Result.ok().data("total", total).data("items", records);
    }

    //根据id查询
    @ApiOperation(value = "根据id查询")
    @GetMapping("/getById/{id}")
    public Result getById(@ApiParam(name = "id", value = "订单id", required = true)
                          @PathVariable String id) {
        return Result.ok().data("order", orderService.getById(id));
    }

    //删除
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@ApiParam(name = "id", value = "订单id", required = true)
                             @PathVariable String id) {
        boolean remove = orderService.removeById(id);
        if (remove) {
            return Result.ok().message("删除订单成功!");
        } else {
            return Result.error().message("删除订单失败!");
        }
    }


}

